<template>
  <div
    class="app-container"
    style="background-color: rgb(245, 247, 249);background: #fff;margin: 10px 10px 0px 10px;border-radius: 10px;"
  >
    <div>
      <el-row class="app-toptool" type="flex">
        <el-col :span="16">
          <Search
            size="small"
            :search-visible="searchVisible"
            :search-data.sync="searchData"
            :search-form="searchForm"
            @refesh_list="searchgo"
          />
        </el-col>
        <el-col :span="8">
          <div class="btn-group" style="margin-bottom: 11px; float: right">
            <div>
              <el-button
                v-if="
                  ('store' != controller && 'technical' != controller) ||
                  checkPermission(`/${controller}/add`)
                "
                type="primary"
                size="small"

                @click="add()"
                >添加
              </el-button>
              <!-- <el-button
              v-if="checkPermission('/diyfields/delete')"
              type="primary"
              :disabled="multiple"
              size="small"
              icon="el-icon-delete"
              @click="del(ids)"
            >删除
            </el-button>
            <el-button
              v-if="checkPermission('/diyfields/importData')"
              type="primary"
              size="small"
              icon="el-icon-upload"
              @click="importData(ids)"
            >导入
            </el-button>
            <el-button
              v-if="checkPermission('/diyfields/dumpdata')"
              type="primary"
              size="small"
              icon="el-icon-download"
              @click="dumpdata(ids)"
            >导出
            </el-button> -->
            </div>
          </div>
        </el-col>
      </el-row>
      <div>
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :row-class-name="rowClass"
          row-key="id"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          :border="false"
          :stripe="true"
          class="eltable"
          :data="list"
          style="width: 100%"
          @selection-change="selection"
        >
          <!-- <el-table-column align="center" type="selection" width="42"/> -->
          <el-table-column
            align="center"
            type=""
            property="id"
            label="编号"
            show-overflow-tooltip
            width="70"
          />
          <template v-for="column in RegisterField">
            <el-table-column
              v-if="column.fieldsmingcheng == 'touxiang'"
              :key="column.fieldsmingcheng"
              :prop="column.inputtype + ',' + column.fieldsmingcheng"
              label="用户信息"
              width="300"
            >
              <template slot-scope="scope">
                <div
                  class="demo-image__preview"
                  style="display: flex; align-items: center"
                >
                  <el-image
                    class="table_list_pic"
                    :src="
                      scope.row[scope.column.property.split(',')[1]] ||
                      require('@/assets/no_touxiang.png')
                    "
                    :preview-src-list="[
                      scope.row[scope.column.property.split(',')[1]] ||
                        require('@/assets/no_touxiang.png'),
                    ]"
                  />
                  <div style="text-align: left; padding-left: 10px">
                    <div>姓名：{{ scope.row.title }}</div>
                    <div>手机号：{{ scope.row.tel }}</div>
                  </div>
                </div>
              </template>
            </el-table-column>

            <el-table-column
              v-else-if="column.fieldsmingcheng == 'store_logo'"
              :key="column.fieldsmingcheng"
              :prop="column.inputtype + ',' + column.fieldsmingcheng"
              label="商家信息"
              width="300"
            >
              <template slot-scope="scope">
                <div
                  class="demo-image__preview"
                  style="display: flex; align-items: center"
                >
                  <el-image
                    class="table_list_pic"
                    :src="
                      scope.row[scope.column.property.split(',')[1]] ||
                      require('@/assets/no_touxiang.png')
                    "
                    :preview-src-list="[
                      scope.row[scope.column.property.split(',')[1]] ||
                        require('@/assets/no_touxiang.png'),
                    ]"
                  />
                  <div style="text-align: left; padding-left: 10px">
                    <div>姓名：{{ scope.row.title }}</div>
                    <div>手机号：{{ scope.row.tel }}</div>
                  </div>
                </div>
              </template>
            </el-table-column>

            <el-table-column
              v-else-if="
                column.fieldsmingcheng != 'tel' &&
                column.fieldsmingcheng != 'title'
              "
              :key="column.fieldsmingcheng"
              :prop="column.inputtype + ',' + column.fieldsmingcheng"
              :label="column.viewmingcheng"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <div v-if="column.fieldsmingcheng == 'cate_ids'">
                  <div
                    v-if="scope.row.cate_ids && scope.row.cate_ids.split(',')"
                  >
                    <el-tag v-for="item in scope.row.cate_ids.split(',')">{{
                      item
                    }}</el-tag>
                  </div>
                </div>
                <div v-else>
                  {{ scope.row[scope.column.property.split(",")[1]] }}
                </div>
              </template>
            </el-table-column>
          </template>
          <el-table-column
            align="center"
            property="status"
            label="状态"
            show-overflow-tooltip
            width="70"
          >
            <template slot-scope="scope">
              {{ scope.row.status == 1 ? "正常" : "隐藏" }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="300">
            <template slot-scope="scope">
              <div v-if="scope.row.id">
                <el-button
                  v-if="
                    checkPermission('/' + controller + '/login') &&
                    'index' == action &&
                    'store' == controller
                  "
                  size="mini"
                  type="primary"
                  @click="login(scope.row)"
                >
                  登录后台
                </el-button>
                <el-button
                  v-if="
                    checkPermission('/' + controller + '/auditdetail') &&
                    'audit' == action
                  "
                  size="mini"
                  type="primary"
                  @click="audit(scope.row)"
                >
                  <i class="el-icon-finished" />审核
                </el-button>

                <el-button
                  v-if="controller == 'technical'"
                  size="mini"
                  type="primary"
                  @click="viewPosition(scope.row)"
                >
                  <i class="el-icon-view" />查看定位
                </el-button>

                <el-button
                  v-if="
                    checkPermission('/' + controller + '/update') &&
                    'index' == action
                  "
                  size="mini"
                  type="primary"
                  @click="update(scope.row)"
                  >编辑
                </el-button>

                <el-button
                  v-if="
                    checkPermission('/' + controller + '/delete') &&
                    'index' == action
                  "
                  size="mini"
                  type="danger"
                  @click="del(scope.row)"
                  >删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          :total="page_data.total"
          :page.sync="page_data.page"
          :limit.sync="page_data.limit"
          @pagination="index"
        />
      </div>
    </div>
    <!--审核-->
    <Auditdetail
      :info="auditInfo"
      :opentype="opentype"
      :show.sync="dialog.auditdetailDialogStatus"
      size="small"
      @refesh_list="index"
    />
    <!--添加/修改-->
    <Update
      :info="updateInfo"
      :opentype="opentype"
      :show.sync="dialog.updateDialogStatus"
      size="small"
      @refesh_list="index"
      @update:show="dialog.updateDialogStatus"
    />
    <Position
      :info="technicalPos"
      :show.sync="dialog.positionDialogStatus"
      @update:show="dialog.positionDialogStatus"
    />
    <!--导入-->
    <ImportData
      :show.sync="dialog.importDataDialogStatus"
      controller="technical"
      size="small"
      @refesh_list="index"
    />
    <el-dialog
      title="导出进度条"
      :visible="dumpshow"
      :before-close="closedialog"
      width="500px"
    >
      <el-progress :percentage="percentage" />
    </el-dialog>
  </div>
</template>
<script>
import Search from "@/components/common/Search";
import Pagination from "@/components/Pagination";
import Auditdetail from "@/views/diyfields/auditdetail.vue";
import ImportData from "@/views/diyfields/importData.vue";
import Update from "@/views/diyfields/update";
import Position from "@/views/technical/position";
import { confirm, param2Obj } from "@/utils/common";
import XLSX from "xlsx";

export default {
  name: "Diyfields",
  components: {
    Search,
    Pagination,
    Update,
    Auditdetail,
    ImportData,
    Position,
  },
  data() {
    return {
      dialog: {
        auditdetailDialogStatus: false,
        importDataDialogStatus: false,
        updateDialogStatus: false,
        positionDialogStatus: false,
      },
      ids: [],
      single: true,
      multiple: true,
      list: [],
      RegisterField: [],
      opentype: "add",
      updateInfo: {},
      auditInfo: {},
      loading: false,
      dumpshow: false,
      exceldata: [],
      dumppage: 1,
      percentage: 0,
      ws: {},
      filename: "",
      page_data: {
        limit: 20,
        page: 1,
        total: 20,
      },
      searchVisible: true,
      searchForm: [],
      searchData: {},
      action: "index",
      controller: "",
      technicalPos: {},
    };
  },
  mounted() {
    this.index();
  },
  methods: {
    viewPosition(row) {
      const id = row.id;
      console.log(id);
      this.$api
        .get("/technical/getPosition", {
          id: id,
        })
        .then((res) => {
          this.dialog.positionDialogStatus = true;
          this.technicalPos = res.data;
        });
    },
    searchgo() {
      this.page_data.page = 1;
      this.index();
    },
    index() {
      const param = {
        path: this.$route.path,
        limit: this.page_data.limit,
        page: this.page_data.page,
      };
      Object.assign(param, this.searchData);
      Object.assign(param, param2Obj(this.$route.fullPath));
      this.loading = true;

      this.$api.post("/diyfields/index", param).then((res) => {
        this.list = res.data.data;
        this.page_data.total = res.data.total;
        this.action = res.action;
        this.controller = res.controller;
        this.loading = false;
        // console.log(this.page_data);
        if (this.page_data.page == 1) {
          this.RegisterField = res.field_data.RegisterField;
          this.searchForm = [
            {
              type: "Select",
              label: "状态",
              prop: "status",
              data: [
                {
                  key: "已通过",
                  val: "1",
                },
                {
                  key: "未审核",
                  val: "0",
                },
              ],
              width: "150px",
            },
            {
              type: "Input",
              label: "请输入查询内容",
              prop: "keyword",
              width: "230px",
            },
          ];
        }
      });
    },
    login(row) {
      const id = row.id ? row.id : this.ids.join(",");
      window.open('?uniacid='+ localStorage.getItem('uniacid') +'&sid=' + id + '#/home', '_blank')

      // window.open("?sid=" + id + "#/home", "_blank");
    },
    listUpdate(row, field) {
      if (row.id) {
        this.$api
          .post("/diyfields/listUpdate", {
            path: this.$route.path,
            id: row.id,
            [field]: row[field],
          })
          .then((res) => {
            this.$message({
              message: "操作成功",
              type: "success",
            });
          });
      }
    },
    audit(row) {
      this.opentype = "update";
      const id = row.id ? row.id : this.ids.join(",");
      this.$api
        .post("/diyfields/getInfo", {
          path: this.$route.path,
          id: id,
        })
        .then((res) => {
          this.dialog.auditdetailDialogStatus = true;
          this.auditInfo = res.data;
        });
    },
    add() {
      this.opentype = "add";
      this.$api
        .post("/diyfields/getInfo", {
          path: this.$route.path,
        })
        .then((res) => {
          this.dialog.updateDialogStatus = true;
          this.updateInfo = res.data;
        });
    },
    update(row) {
      this.opentype = "update";
      const id = row.id ? row.id : this.ids.join(",");
      this.$api
        .post("/diyfields/getInfo", {
          path: this.$route.path,
          id: id,
        })
        .then((res) => {
          this.dialog.updateDialogStatus = true;
          this.updateInfo = res.data;
        });
    },
    del(row) {
      confirm({
        content: "确定要操作吗",
      })
        .then(() => {
          const ids = row.id ? row.id : this.ids.join(",");

          this.$api
            .post("/diyfields/delete", {
              path: this.$route.path,
              id: ids,
            })
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.index();
            });
        })
        .catch(() => {});
    },
    importData() {
      this.dialog.importDataDialogStatus = true;
    },
    dumpdata() {
      confirm({
        content: "确定要操作吗",
      })
        .then(() => {
          this.dumpshow = true;
          this.confirmdumpdata();
        })
        .catch(() => {});
    },
    confirmdumpdata() {
      let param = {
        path: this.$route.path,
        page: this.dumppage,
      };
      if (this.searchData) param = Object.assign(param, this.searchData);
      this.$api.post("/diyfields/dumpdata", param).then((res) => {
        if (res.data.length > 0) {
          if (this.dumppage == 1) {
            this.exceldata.push(res.header);
          }
          res.data.forEach((item) => {
            this.exceldata.push(Object.values(item));
          });
          this.percentage = res.percentage;
          this.filename = res.filename;
          this.ws = XLSX.utils.aoa_to_sheet(this.exceldata);
          this.dumppage = this.dumppage + 1;
          this.confirmdumpdata();
        } else {
          const wb = XLSX.utils.book_new();
          XLSX.utils.book_append_sheet(wb, this.ws, "资料");
          XLSX.writeFile(wb, this.filename);
          this.dumpshow = false;
        }
      });
    },
    closedialog() {
      this.dumpshow = false;
    },
    selection(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    rowClass({ row, rowIndex }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return "rowLight";
        }
      }
    },
  },
};
</script>
<style scoped>
.table_list_pic {
  width: 60px;
  vertical-align: middle;
}
</style>
